package com.kelaite.procurement.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.constants.Constants;
import com.kelaite.procurement.constants.VendorGradeEnum;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TVendor;
import com.kelaite.procurement.repository.VendorRepository;
import com.kelaite.procurement.service.VendorService;
import com.kelaite.procurement.utils.ConvertTime;

@Service
@Transactional
public class VendorServiceImpl implements VendorService{
  
	@Autowired
	private VendorRepository vendorRepository; 
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TVendor save(TVendor persistence) {
		return this.vendorRepository.save(persistence);
	}

	@Override
	public TVendor getObject(Long id) {
		return this.vendorRepository.findOne(id);
	}

	@Override
	public List<TVendor> getAll() {
		return this.vendorRepository.findAll();
	}

	@Override
	public void deleteByObject(TVendor persistence) {
		this.vendorRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.vendorRepository.delete(id);
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<TVendor> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TVendor.class).add(org.hibernate.criterion.Expression.eq(property, value));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public TVendor getVendorByClientAndSupplier(TCompany client,
			TCompany supplier) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TVendor.class)
				.add(Restrictions.eq("tClient", client))
				.add(Restrictions.eq("tSupplier", supplier));
		List<TVendor> vendorList = criteria.list();
		if(!vendorList.isEmpty()){
			return vendorList.get(0);
		}
		return null;
	}
	
	/**
	 * �������
	 * @param id
	 * @param grade
	 * @param status
	 */
	@Override
	public void updateVendorGrade(Long id, String grade, String status) {
		
		short tempStatus=new Short(status);
		switch(grade)
		{
			case "1":
				grade="E";
				grade=VendorGradeEnum.getCode(VendorGradeEnum.E);
				tempStatus=4;
				break;
			case "2":
				grade="D";
				grade=VendorGradeEnum.getCode(VendorGradeEnum.D);
				break;
			case "3":
				grade="C";
				grade=VendorGradeEnum.getCode(VendorGradeEnum.C);
				break;
			case "4":
				grade="B";
				grade=VendorGradeEnum.getCode(VendorGradeEnum.B);
				break;
			case "5":
				grade="A";
				grade=VendorGradeEnum.getCode(VendorGradeEnum.A);
				break;
		}
		Date currentDate=new Date();
		
		Date oneYearDate = ConvertTime.plusDaysDate(currentDate, Constants.ONE_YEAR);
		this.vendorRepository.updateVendorGrade(id, grade, tempStatus, currentDate,oneYearDate);
		
	}
	
}
